package com.sy.notice.biz.eventnotice.service;

import com.sy.notice.SendMsgToBark;
import com.sy.notice.SendMsgToDingTalk;
import com.sy.notice.TxSendSms;
import com.sy.notice.biz.eventdefine.entity.EventTask;
import com.sy.notice.biz.eventnotice.properties.DingTalkProperties;
import com.sy.notice.biz.eventnotice.properties.TxSmsProperties;
import com.sy.notice.msg.TextMessage;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 舒祥
 * @date 2022-01-14 21:20
 * @desc 事件管理器
 */
@Slf4j(topic = "notice_log")
public class EventNoticeManager {

    private DingTalkProperties dingTalkProperties;

    private TxSmsProperties txSmsProperties;

    public EventNoticeManager(DingTalkProperties dingTalkProperties, TxSmsProperties txSmsProperties) {
        this.dingTalkProperties = dingTalkProperties;
        this.txSmsProperties = txSmsProperties;
    }

    /**
     * 发送通知 0:短信    1:钉钉   2:bark
     *
     * @param eventTask
     */
    public void sendMsg(EventTask eventTask) {
        Integer eventNoticeType = eventTask.getEventNoticeType();
        switch (eventNoticeType) {
            case 0:
                smsTx(eventTask);
                break;
            case 1:
                dingTalk(eventTask);
                break;
            case 2:
                barkApp(eventTask);
                break;
            case 999:
                smsTx(eventTask);
                dingTalk(eventTask);
                barkApp(eventTask);
                break;
            default:
                throw new RuntimeException("not fount noticeType");
        }
    }

    private void barkApp(EventTask eventTask) {
        try {
            SendMsgToBark sendMsgToBark = new SendMsgToBark();
            String noticeAppSK = eventTask.getNoticeAppSk();
            if (StringUtils.isEmpty(noticeAppSK)) {
                throw new IllegalArgumentException("noticeAppSK must exist");
            }
            String[] appSKSet = noticeAppSK.split(",");
            for (String s : appSKSet) {
                String res = sendMsgToBark.sendMsg(eventTask::getEventDesc, () -> s);
                log.info("bark send msg sk:{}, res:{} ", s, res);
            }
        } catch (Exception e) {
            log.error("bark fail eventTask:{}", eventTask, e);
        }
    }

    /**
     * 腾讯云短信服务
     *
     * @param eventTask
     */
    private void smsTx(EventTask eventTask) {
        try {
            TxSendSms txSendSms = new TxSendSms();
            String noticePhone = eventTask.getNoticePhone();
            if (StringUtils.isEmpty(noticePhone)) {
                throw new IllegalArgumentException("noticePhone must exist");
            }
            String[] phoneNumberSet = noticePhone.split(",");
            SendSmsResponse sendSmsResponse = txSendSms.sendMsg(
                    //templateParamSet 针对短信模板来 我的模板就一个变量因此就一个参数 todo
                    () -> new String[]{eventTask.getEventDesc()},
                    () -> {
                        Map map = new HashMap<>(6);
                        map.put("secretId", txSmsProperties.getSecretId());
                        map.put("secretKey", txSmsProperties.getSecretKey());
                        map.put("smsSdkAppId", txSmsProperties.getSdkAppId());
                        map.put("signName", txSmsProperties.getSignName());
                        map.put("templateId", txSmsProperties.getTemplateId());
                        map.put("phoneNumberSet", phoneNumberSet);
                        return map;
                    });
            log.info("tx send sms :{} ", SendSmsResponse.toJsonString(sendSmsResponse));
        } catch (Exception e) {
            log.error("sms tx fail eventTask:{}", eventTask, e);
        }
    }

    /**
     * 钉钉群机器人
     *
     * @param eventTask
     */
    private void dingTalk(EventTask eventTask) {
        try {
            SendMsgToDingTalk sendMsgToDingTalk = new SendMsgToDingTalk();
            TextMessage textMessage = wrapTextMsg(eventTask);
            boolean res = sendMsgToDingTalk.sendMsg(() -> textMessage, () -> {
                HashMap<Object, Object> map = new HashMap<>(2);
                map.put("accessToken", dingTalkProperties.getAccessToken());
                map.put("secret", dingTalkProperties.getSecret());
                return map;
            });
            log.info("dingTalk send result:{}, msg:{}", res, textMessage);
        } catch (Exception e) {
            log.error("dingtalk fail eventTask:{}", eventTask, e);
        }
    }

    private TextMessage wrapTextMsg(EventTask eventTask) {
        String noticePhone = eventTask.getNoticePhone();
        TextMessage textMessage = new TextMessage();
        TextMessage.Text text = new TextMessage.Text();
        text.setContent(eventTask.getEventDesc());
        textMessage.setText(text);
        if (!StringUtils.isEmpty(noticePhone)) {
            String[] split = noticePhone.split(",");
            TextMessage.At at = new TextMessage.At();
            at.setAtMobiles(Arrays.asList(split));
            textMessage.setAt(at);
        }
        return textMessage;
    }

}
